<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="120px" size="mini">
      <el-form-item label="配件名称" prop="name">
        <el-input v-model="formItem.name" placeholder="请输入配件名称" />
      </el-form-item>
      <el-form-item label="配件分类" prop="spareTypeCode">
        <TreeSelect
          v-model="formItem.spareTypeCode"
          :multiple="false"
          :options="typeOptions"
          placeholder="选择配件类型"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="specification">
        <el-input v-model="formItem.specification" placeholder="请输入规格型号" />
      </el-form-item>
      <!-- <el-form-item label="所属资产" prop="assetCode">
        <el-input v-model="formItem.assetCode" placeholder="请输入资产编码" />
      </el-form-item> -->
      <el-form-item label="配件描述" prop="description">
        <el-input v-model="formItem.description" placeholder="请输入配件描述" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import TreeSelect from '@neu/biz-core/components/TreeSelect'

import {
  tree
} from '@neu/dcam-ui/api/spareType'

// 定义一个表单原始项
const formItem = {
  spareTypeCode: null,
  spareTypeName: null,
  name: '',
  specification: '',
  description: '',
  assetCode: ''
}
export default {
  name: 'GoodsFileEditForm',
  components: {
    TreeSelect
  },
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      // 控件类型
      controlTypeList: [],
      // 文本格式下拉框
      valueTypeList: [],
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: {
        name: [
          { required: true, message: '配件名称不能为空', trigger: 'blur' },
          { max: 225, message: '长度不能超过225', trigger: 'blur' }
        ],
        specification: [
          { required: true, message: '规格型号不能为空', trigger: 'blur' },
          { max: 225, message: '长度不能超过225', trigger: 'blur' }
        ],
        spareTypeCode: [
          { required: true, message: '配件分类不能为空', trigger: 'blur' }
        ]
      },
      modal: true,
      /** 备件类型树 */
      typeOptions: []
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: {
      handler(newVal, oldVal) {
        this.fetch()
      },
      immediate: true
    }
  },
  created() {
    this.tree()
  },
  methods: {
    // 类型树
    tree() {
      tree().then(response => {
        this.typeOptions = response.data
      })
    },
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        this.formItem = this.$parent.$parent.$parent.selectGoodsFileRow
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            // 查询配件类型名称
            const flatTree = this.flattenTreeDataClosure(this.typeOptions)
            const one = flatTree.find(s => s.code === this.formItem.spareTypeCode)
            this.formItem.spareTypeName = one ? one.title : ''
            resolve(this.formItem)
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
    },
    // 扁平化树形结构
    flattenTreeDataClosure(data) {
      const treeData = data
      const flattenData = []
      function flattenTree(data, parentKey) {
        data.forEach(ele => {
          const { title, code, children } = ele
          flattenData.push({ title, code, parentKey })
          if (children) {
            flattenTree(children, code)
          }
        })
      }
      flattenTree(treeData, null)
      return flattenData
    }
  }
}
</script>
